package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.meeting.Share.model.ShareAbleInfo;
import com.device.idea.meeting.Share.model.ShareAppInfo;
import com.device.idea.meeting.Share.model.ShareCastInfo;
import com.device.idea.meeting.Share.model.ShareType;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.consts.ConfConst;
import com.huawei.chengdu.meetingdemo.model.ShareDataInfo;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.view.adapter.ShareItemAdapter;

import java.util.ArrayList;
import java.util.List;

public class ShareItemSelectDialog extends Dialog {
    private ImageView dialogClose;
    private RecyclerView shareItemList;
    private Context mContext;
    private ShareItemAdapter shareItemAdapter;

    public ShareItemSelectDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_share);
        initView();
        initListener();
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        refreshData();
        super.show();
    }

    private void initView() {
        dialogClose = findViewById(R.id.share_dialog_close);
        shareItemList = findViewById(R.id.share_item_list);
        shareItemList.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void refreshData() {
        ShareAbleInfo shareList = ConfSdkPresenter.getShareList();
        LogUtil.info("ShareAbleInfo = " + shareList);
        List<ShareDataInfo> shareDataInfoList = new ArrayList<>();
        for (ShareCastInfo shareCastInfo : shareList.getCastList()) {
            ShareDataInfo shareDataInfo = new ShareDataInfo();
            shareDataInfo.setShareType(ShareType.CAST);
            shareDataInfo.setShareCastInfo(shareCastInfo);
            shareDataInfoList.add(shareDataInfo);
        }
        for (ShareAppInfo shareAppInfo : shareList.getAppList()) {
            if (ConfConst.EXCLUDE_APPS.contains(shareAppInfo.getAppPackName())) {
                continue;
            }
            ShareDataInfo shareDataInfo = new ShareDataInfo();
            shareDataInfo.setShareType(ShareType.APP);
            shareDataInfo.setShareAppInfo(shareAppInfo);
            shareDataInfoList.add(shareDataInfo);
        }
        // shareList中没有白板，单独增加白板item
        addWhiteBoardItem(shareDataInfoList);
        if (shareItemAdapter == null) {
            shareItemAdapter = new ShareItemAdapter(shareDataInfoList, mContext);
            shareItemList.setAdapter(shareItemAdapter);
        } else {
            shareItemAdapter.updateShareDataList(shareDataInfoList);
        }
    }

    private void addWhiteBoardItem(List<ShareDataInfo> shareDataInfoList) {
        ShareCastInfo whiteboardInfo = new ShareCastInfo();
        ShareDataInfo shareDataInfo = new ShareDataInfo();
        shareDataInfo.setShareType(ShareType.WHITEBOARD);
        shareDataInfo.setShareCastInfo(whiteboardInfo);
        shareDataInfoList.add(shareDataInfo);
    }

    private void initListener() {
        dialogClose.setOnClickListener(v -> dismiss());
    }
}
